import { Toast } from "vant";
import { scrollMixin } from "utils/mixin";
import cFlow from "components/Waterfalls_flow";
import microCommunities from "components/micro_communities";
import U_foot from "components/new_diy/foot";

//import BMap from 'BMap';
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      head_top:0,//渐变色元素距离顶部的高度
      bg_height:0,
      active: 0,
      topContent: {},
      bannerContent: {},
      functionList: {},
      cate: [],
      cateList: [],
      newClassification: "",
      nearbyClassification: "",
      address: "",
      city: "",
      point: "",
      poster: "",
      //more
      isLoadMore: true,
      page: 1,
      total_page: 0,
      getMoreDataType: "newClassification",
      cateMoreData: {},
      showLoading: false,

      show_video_icon:"",
      show_sign_icon:"",
      show_qr_icon:"",
      show_clock_icon:"",

      newArray1: [],
      newArray2: [],
      video_path:'',
      hide_comment:1,//1-不显示、0-显示
      micro_style_type:0,//0样式一 |1样式二
      closeNearby: false //是否关闭附近
    };
  },
  deactivated() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  },
  activated() {
    // this.getHomePageData();
  },
  created() {
    this.getHomePageData();
  },
  mounted() {

  },
  methods: {
    get_elem_style(){
      if(this.$refs.swipeRoot){
          this.head_top = (this.$refs.swipeRoot.offsetHeight / 2 ) + this.$refs.swipeRoot.offsetTop;
          let van_tabs__wrap = document.getElementsByClassName('van-tabs__wrap')[0];
          if(van_tabs__wrap){
            if(this.active == '0' && this.micro_style_type == 0){//模板一
              let waterfalls_flow = document.getElementById('Waterfalls_flow')
                let _li = waterfalls_flow&&waterfalls_flow.getElementsByTagName('li');
                if(_li){
                  let all_li = [..._li];
                  let _all_offsetHeight = [];
                  all_li.forEach(item=>{
                    _all_offsetHeight.push(parseInt(item.offsetHeight));
                  })
                  if(_all_offsetHeight){
                    let li_height = Math.max.apply(null,_all_offsetHeight) / 2;
                    this.bg_height =(this.$refs.swipeRoot.offsetHeight / 2)+van_tabs__wrap.offsetHeight + li_height;
                  }
                }
            }else{
              let post_item = document.getElementsByClassName('post-item')[0];
              this.bg_height = (this.$refs.swipeRoot.offsetHeight / 2)+(post_item&&post_item.offsetHeight/2)+van_tabs__wrap.offsetHeight;
            }
          }
      }
    },
     kFormatter(num) {
      return Math.abs(num) > 999 ? Math.sign(num)*((Math.abs(num)/1000).toFixed(2)) + 'k' : Math.sign(num)*Math.abs(num)
    },
    getHomePageData() {
      $http.get("plugin.micro-communities.api.index.index").then(
        (response)=> {
          if (response.result == 1) {
            if (response.data.set) {
              let set = response.data.set;
              this.topContent = {
                micro_top: response.data.set.micro_top,
                micro_top_jump: response.data.set.micro_top_jump,
                browse_num: response.data.browse_num,
                trick_num: response.data.trick_num
              };
              this.bannerContent = {
                micro_home_one: response.data.set.micro_home_one,
                micro_home_one_jump: response.data.set.micro_home_one_jump,
                micro_home_two: response.data.set.micro_home_two,
                micro_home_two_jump: response.data.set.micro_home_two_jump,
                micro_home_three: response.data.set.micro_home_three,
                micro_home_three_jump: response.data.set.micro_home_three_jump
              };
              this.functionList = {
                show_sign: response.data.set.show_sign == 1 ? true : false,
                show_clock: response.data.set.show_clock == 1 ? true : false,
                show_video: response.data.set.show_video == 1 ? true : false,
                show_qr: response.data.set.show_qr == 1 ? true : false
              };
              this.micro_style_type = response.data.set.micro_style_type
              this.show_video_icon = set.show_video_icon;
              this.show_sign_icon = set.show_sign_icon;
              this.show_qr_icon = set.show_qr_icon;
              this.show_clock_icon = set.show_clock_icon;
            }
            if(response.data.video_set){
              if(response.data.video_set.video_path){
                this.video_path = response.data.video_set.video_path;
              }
            }
            this.cate = response.data.cate;
            for (let i = 0; i < this.cate.length; i++) {
              this.cateList.push([]);
            }

            let is_default = true;
            if(this.fun.getKey('cat_id')) {
              // 通过分类id进入
              let cate_index = null;
              let child_flag = false;
              this.cate.map((item,i)=> {
                if(item.id == this.fun.getKey('cat_id')) {
                  cate_index = i;
                }
                item.child.map((child)=> {
                  if(child.id == this.fun.getKey('cat_id')) {
                    cate_index = i;
                    child_flag = true;
                  }
                });
              });
              if(cate_index !== null) {
                is_default = false;
                this.$nextTick(()=> {
                  this.active = cate_index + 2;
                  if(child_flag) {
                    this.getCateChild(this.fun.getKey('cat_id'));
                  }else {
                    this.getClassificationBtn(this.active,'');
                  }
                });
              }
            }

            if(is_default){
              this.getClassification("newClassification");
            }

          } else {
            Toast(response.msg);
          }
          if (response.data.video_set) {
            if (response.data.video_set.video_path) {
              this.video_path = response.data.video_set.video_path;
            }
          }
          this.cate = response.data.cate;
          this.closeNearby = response.data.nearby_close ? Number(response.data.nearby_close) : false;
          if (!this.closeNearby) this.locationInit();
          for (let i = 0; i < this.cate.length; i++) {
            this.cateList.push([]);
          }

          let is_default = true;
          if (this.fun.getKey("cat_id")) {
            // 通过分类id进入
            let cate_index = null;
            let child_flag = false;
            this.cate.map((item, i) => {
              if (item.id == this.fun.getKey("cat_id")) {
                cate_index = i;
              }
              item.child.map(child => {
                if (child.id == this.fun.getKey("cat_id")) {
                  cate_index = i;
                  child_flag = true;
                }
              });
            });
            if (cate_index !== null) {
              is_default = false;
              this.$nextTick(() => {
                this.active = this.closeNearby ? cate_index + 1 : cate_index + 2;
                if (child_flag) {
                  this.getCateChild(this.fun.getKey("cat_id"));
                } else {
                  this.getClassificationBtn(this.active, "");
                }
              });
            }
          }

          if (is_default) {
            this.getClassification("newClassification");
          }


      });
    },
    locationInit() {
      this.fun
        .getLocation()
        .then(res => {
          this.city = res.city;
          this.address = res.title;
          this.point = res.point;
        })
        .catch(err => {
          console.log(err);
        });
    },
    jumpAddress(url) {
      if (url) {
        window.location.href = url;
      }
    },
    goClassification(id) {
      this.$router.push(
        this.fun.getUrl("microClassification",{}, {
          cateId: id,
          micro_style_type:this.micro_style_type
        })
      );
    },
    getClassificationBtn(index, title) {
      if (title == "最新") {
        this.getClassification("newClassification");
        this.getMoreDataType = "newClassification";
      } else if (title == "附近") {
        this.getClassification("nearbyClassification");
        this.getMoreDataType = "nearbyClassification";
      } else {
        let indexData = this.closeNearby ? index - 1 : index - 2;
        this.getClassification("", this.cate[indexData].id, indexData);
        this.getMoreDataType = "cate";
        this.cateMoreData = {
          type: "",
          id: this.cate[indexData].id,
          index: indexData
        };
      }
    },
    getClassification(type, id, index) {
      let dataJson = {};
      this.showLoading = false;
      this.isLoadMore = false;
      this.pageInit();
      if (id) {
        dataJson.cat_id = id;
      } else {
        if (type == "newClassification") {
          dataJson.trick_new = 1;
        } else if (type == "nearbyClassification") {
          let p = this.fun.bd_encrypt(this.point.lng, this.point.lat);
          dataJson.lng = p.lng;
          dataJson.lat = p.lat;
          dataJson.city_name = this.city;
          dataJson.address = this.city;
        }
      }
      dataJson.stick_type = 1;
      $http.post("plugin.micro-communities.api.index.getStick", dataJson, "").then(response => {
        this.showLoading = true;
        if (response.result == 1) {
          this.total_page = response.data.last_page;
          this.hide_comment = response.data.hide_comment
          if (id) {
            this.cateList.splice(index, 1, response.data.data);
          } else {
            if (type == "newClassification") {
              this.newClassification = response.data.data;
            } else if (type == "nearbyClassification") {
              this.nearbyClassification = response.data.data;
            }
          }
          this.get_elem_style();
        } else {
          Toast(response.msg);
          this.showLoading = true;
          this.isLoadMore = false;
        }

      });
    },
    getCateChild(id) {
      let indexData = this.closeNearby ? this.active - 1 : this.active - 2;
      this.getCateChildData("", id, indexData);
      this.getMoreDataType = "cate";
      this.cateMoreData = {
        type: "",
        id: id,
        index: indexData
      };
    },
    getCateChildData(type, id, index) {
      let dataJson = {};
      this.showLoading = false;
      this.isLoadMore = false;
      this.pageInit();
      if (id) {
        dataJson.cat_id = id;
      }
      this.cateList[index] = [];
      dataJson.stick_type = 1;
      $http.post("plugin.micro-communities.api.index.getStick", dataJson, "").then(response => {
        this.showLoading = true;
        if (response.result == 1) {
          this.total_page = response.data.last_page;
          if (id) {
            this.cateList.splice(index, 1, response.data.data);
          }
          this.get_elem_style();
        } else {
          Toast(response.msg);
          this.showLoading = true;
          this.isLoadMore = false;
        }
      });
    },
    jumpDetails(stick_id) {
      this.$router.push(
        this.fun.getUrl("microCommentDetails", {
          stickId: stick_id
        })
      );
    },
    jumpToSearch() {
      this.$router.push(this.fun.getUrl("microSearchIndex"));
    },
    toVideo() {
      console.log(this.video_path);
      if (this.video_path == "videoDetail") {
        this.$router.push(this.fun.getUrl("VideoDetail"));
      } else {
        this.$router.push(this.fun.getUrl("VideoList"));
      }
    },
    goToUrl(name) {
      this.$router.push(this.fun.getUrl(name));
    },
    openQrCode(e) {
      if (!this.poster && e !== "none") {
        if (!this.timer) {
          this.getPoster(e);
          this.timer = setInterval(() => {
            this.getPoster(e);
          }, 3000);
        } else {
          this.$refs.hook.style.display = e;
        }
      } else {
        this.$refs.hook.style.display = e;
        clearInterval(this.timer);
      }
    },
    getPoster(e) {
      $http.get("member.poster").then(
        response => {
          if (response.result === 1) {
            this.poster = response.data.image_url;
            this.$refs.hook.style.display = e;
            if (this.poster && this.timer) {
              clearInterval(this.timer);
            }
          } else {
            Toast(response.msg);
            if (this.timer) {
              clearInterval(this.timer);
            }
          }
        },
        response => {
          console.log(response.msg);
          if (this.timer) {
            clearInterval(this.timer);
          }
        }
      );
    },
    pageInit() {
      this.page = 1;
      this.total_page = 0;
      this.isLoadMore = true;
      //this.newClassification = [];
    },
    loadMore() {
      this.isLoadMore = true;
    },
    //获取更多数据
    getMoreData() {
      let json = {};
      if (this.getMoreDataType == "newClassification") {
        json.trick_new = 1;
      } else if (this.getMoreDataType == "nearbyClassification") {
        let p = this.fun.bd_encrypt(this.point.lng, this.point.lat);
        json.lng = p.lng;
        json.lat = p.lat;
        json.city_name = this.city;
        json.address = this.city;
      } else {
        json.cat_id = this.cateMoreData.id;
      }
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        // this.showLoading = true;
        return;
      } else {
        this.page = this.page + 1;
        json.page = this.page;
        this.showLoading = false;
        json.stick_type = 1;
        $http.post("plugin.micro-communities.api.index.getStick", json, "加载中...").then(
          response => {
            this.showLoading = true;
            if (response.result === 1) {
              var myData = response.data.data;
              if (this.getMoreDataType == "newClassification") {
                this.newClassification = this.newClassification.concat(myData);
              } else if (this.getMoreDataType == "nearbyClassification") {
                this.nearbyClassification = this.nearbyClassification.concat(myData);
              } else {
                this.cateList[this.cateMoreData.index] = this.cateList[this.cateMoreData.index].concat(myData);
              }
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
            }
          },
          response => {
            this.showLoading = true;
          }
        );
      }
    }
  },
  components: {
    cFlow,
    microCommunities,
    U_foot
  }
};
